{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f7c037ae",
   "metadata": {},
   "source": [
    "# 循环结构\n",
    "* 循环的分类：\n",
    " * while\n",
    " * for - in\n",
    "* 语法的结构  \n",
    "while条件表达式：  \n",
    " 条件执行体（循环）\n",
    "\n",
    "* 选择结构的if与循环结构while的区别\n",
    " * if是判断一次，条件为true再执行一次\n",
    " * while是判断n+1次，条件为true执行n次\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "710898ec",
   "metadata": {},
   "source": [
    "## 简单的循环函数之和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "16b0b136",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "和为 10\n"
     ]
    }
   ],
   "source": [
    "# 用于存储累加和\n",
    "sum=0\n",
    "'''初始化变量为0'''\n",
    "a=0\n",
    "'''条件判断'''\n",
    "# if a<10:\n",
    "while a<5:\n",
    "    '''条件执行体（循环体）'''\n",
    "    sum+=a\n",
    "    a+=1\n",
    "print('和为',sum)\n",
    "\n",
    "# 该运行首先需要建立存储累加和库sum，设置初始值a=0，再用while循环，当a<5时运行的情况，设限最大值，循环的过程是：\n",
    "# 先进行a+sum=a，然后a+1，a<5,true继续循环，直到a=5时暂停,得出最终的结果\n",
    "# a=0,0+0=0,a=0+1;a=1,1(a)+0(sum)=1,1(a)+1=2(sum);  2<5,2+2=4,2+1=3;3<5(true),3+3=6,3+1=4;4<5,6+4=10,4+1=5;5=5(false)   print:10b  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fbd2d27",
   "metadata": {},
   "source": [
    "## 计算1~100的偶数之和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e59588b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1~100的偶数之和为: 2450\n"
     ]
    }
   ],
   "source": [
    "# 存储累加库\n",
    "sum=0\n",
    "# 初始化变量\n",
    "a=0\n",
    "# 添加一个while循环并设置a的范围\n",
    "while a<100:\n",
    "#     if a%2==0:\n",
    "#     如果要改成奇数之和,要改回偶数的话在bool(布尔值)前面加上not\n",
    "    if not bool(a%2):\n",
    "        sum += a\n",
    "    a+=1\n",
    "print('1~100的偶数之和为:',sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d130f7f",
   "metadata": {},
   "source": [
    "## if 循环 (单分支结构）\n",
    "\n",
    "<b> 1.单分支结构指的是只用if去执行  \n",
    "<b> 2.双分支结构指的是if...else  二选一去执行  \n",
    "<b> 3.多分支结构指的是多执行体    if...elif...elif..else\n",
    "\n",
    " <h3> 错误释义</h3>\n",
    "  * invalid syntax  指的是缩进错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3da0dcca",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入取款金额1200\n",
      "您取款金额大于账户余额\n"
     ]
    }
   ],
   "source": [
    "money = 1000 # 初始金额\n",
    "takeout =int(input('请输入取款金额')) # 想要取款的金额\n",
    "\n",
    "if money >= takeout:\n",
    "    money = money-takeout\n",
    "    print('取款成功，当前余额为',money)\n",
    "else:\n",
    "    print('您取款金额大于账户余额')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "234d38fb",
   "metadata": {},
   "source": [
    "### 判断奇数还是偶数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "22ae2b52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个整数23455552\n",
      "23455552 是偶数，因为可以被2整除\n"
     ]
    }
   ],
   "source": [
    "nub = int(input('请输入一个整数'))\n",
    "# 接下来是判断奇数还是偶数\n",
    "if nub%2 == 0:\n",
    "    print(nub , '是偶数，因为可以被2整除')\n",
    "else:\n",
    "        print(nub , '是奇数')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1df2511",
   "metadata": {},
   "source": [
    "### 多分支使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8b2afd66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的成绩进行判定:480\n",
      "您分值表现为及格\n"
     ]
    }
   ],
   "source": [
    "mark = int(input('请输入您的成绩进行判定:')) # int是将输入的结果转成int类型\n",
    "if mark >=650:\n",
    "    print('您分值表现为优秀');\n",
    "elif 550 <= mark < 650:     # 也可以表达为550 <= mark and  650>mark:\n",
    "    print('您分值表现为良好');\n",
    "elif 480 <= mark < 550:\n",
    "    print('您分值表现为及格');\n",
    "else:\n",
    "    print(\"您分值表现为不及格\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "644b6523",
   "metadata": {},
   "source": [
    "### 嵌套  if\n",
    "* 嵌套  if\n",
    "    * 语法结构：\n",
    "    if 条件表达式1：  \n",
    "        if 内层条件表达式：  \n",
    "            内存条件执行体1  \n",
    "        else：  \n",
    "            内存条件执行体2  \n",
    "     else：   \n",
    "         条件执行体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "45b592e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请问您是会员吗（请输入yes or no）：yes\n",
      "请输入您当前所购物的金额:10000\n",
      "您将享受打八折的优惠： 8000.0\n"
     ]
    }
   ],
   "source": [
    "# vip = input('你是会员吗？yes/no')\n",
    "# # 判断是否会员用户\n",
    "# if vip == 'yes':\n",
    "#     print('会员')\n",
    "# else:   # 非会员\n",
    "#     print('非会员')\n",
    "\n",
    "# 尝试增加金额打折\n",
    "vip = input('请问您是会员吗（请输入yes or no）：')\n",
    "# 判断是否会员用户\n",
    "money = int(input(\"请输入您当前所购物的金额:\"))\n",
    "# 设置条件，如果是会员的话，会出现什么样的打折情况\n",
    "if vip == 'yes':\n",
    "    # 设置打折条件输出结果\n",
    "    if money >= 200:\n",
    "        print(\"您将享受打八折的优惠：\", money*0.8)\n",
    "    elif 100 <= money < 200:\n",
    "        print(\"您将销售打9折的优惠：\", money*0.9)\n",
    "    if money < 100:\n",
    "        print(\"你所支出的金额为（一百以内没有优惠噢！）：\", money)\n",
    "else:   # 非会员\n",
    "    print('非会员')\n",
    "    if money > 200:\n",
    "        print(\"打95折，您的实际支付金额为\", money*0.95)\n",
    "    else:\n",
    "        print(\"您当前的支付金额为\", money)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77065ab3",
   "metadata": {},
   "source": [
    "### 条件表达式\n",
    "#### 整数大小的比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "66f2b7fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入整数2131\n",
      "请输入整数23424\n",
      "2131 小于 23424\n"
     ]
    }
   ],
   "source": [
    "nub_a = int(input('请输入整数a'))\n",
    "nub_b = int(input('请输入整数b'))\n",
    "if nub_a > nub_b:\n",
    "    print(nub_a,'大于',nub_b)\n",
    "else:\n",
    "    print(nub_a,'小于',nub_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8af4ce7",
   "metadata": {},
   "source": [
    "#### pass 的使用\n",
    "<b>pass语句，什么都不做，只是一个占位符，用到需要些语句的地方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e5e545af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您是否为军人，请输入“yes”或者“no”no\n",
      "您进入的是普通人通道，请正常排位\n"
     ]
    }
   ],
   "source": [
    "svip = input('您是否为军人，请输入“yes”或者“no”')\n",
    "if svip == 'yes':\n",
    "    pass\n",
    "# 这里没有想好如果条件成立，下个步骤是什么，那就用pass来暂停运行\n",
    "else:\n",
    "    print('您进入的是普通人通道，请正常排位')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
